﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GlobalGameJam
{
    class Bubble : Plateform
    {
        Texture2D normal;
        Color[] dataNormal;
        Color[] dataPopping;
        Texture2D popping;
        Texture2D pop;
        Color[] dataPop;
        Vector2 moveSpeedNormal;
        bool popped;
        float _timer = 0;
        float timeBeforePop;
        public Bubble(GameplayScreen game)
            : base(game)
        {
        }
        public override void Initialize(Texture2D sprite, Vector2 position, int nbRefresh, Vector2 speed)
        {
            moveSpeedNormal = speed;
            normal = sprite;
            popping = sprite;
            this.sprite = sprite;
            dataNormal = new Color[sprite.Width * sprite.Height];
            normal.GetData(dataNormal);
            dataPopping = new Color[sprite.Width * sprite.Height];
            popping.GetData(dataPopping);
            base.Initialize(sprite, position, nbRefresh, speed);
            timeBeforePop = 600;
        }
        public override bool collide()
        {
            if (!popped)
            {
                sprite = popping;
                data = new Color[sprite.Width * sprite.Height];
                data = dataPopping;
                moveSpeed = new Vector2(0, 0);
                popped = true;
                _timer = timeBeforePop;
            }
            return true;
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (this.currentNbRefresh < this.nbRefresh)
                this.position += this.moveSpeed;
            else
            {
                if (0 >= _timer && !popped)
                {
                    _game.notifyEvent("bubblePop");
                    moveSpeed = new Vector2(0, 0);
                    _timer = timeBeforePop / 3;
                    popped = true;
                    sprite = pop;
                    data = new Color[sprite.Width * sprite.Height];
                    data = dataPop;
                    this.UpdateHitBox();
                }
                else if (_timer <= 0)
                {
                    moveSpeed = moveSpeedNormal;
                    sprite = normal;
                    data = new Color[sprite.Width * sprite.Height];
                    data = dataNormal;
                    _timer = 0;
                    popped = false;
                    this.position = this.moveStart;
                    this.UpdateHitBox();
                    nbRefresh = 0;
                }
            }
            this.UpdateHitBox();

            _timer -= gameTime.ElapsedGameTime.Milliseconds;
            if (_timer < timeBeforePop / 3 && popped)
            {
                if (sprite != pop)
                    _game.notifyEvent("bubblePop");
                sprite = pop;
                data = new Color[sprite.Width * sprite.Height];
                data = dataPop;
                this.UpdateHitBox();
            }
            if (_timer < 0 && popped)
            {
                currentNbRefresh = 0;
                moveSpeed = moveSpeedNormal;
                sprite = normal;
                data = new Color[sprite.Width * sprite.Height];
                data = dataNormal;
                this.position = this.moveStart;
                popped = false;
                this.UpdateHitBox();
            }
            currentNbRefresh++;
        }
        public void setPopping(Texture2D popping)
        {
            dataPopping = new Color[popping.Width * popping.Height];
            popping.GetData(dataPopping);
            this.popping = popping;
        }
        public void setPop(Texture2D pop)
        {
            dataPop = new Color[pop.Width * pop.Height];
            popping.GetData(dataPopping);
            this.pop = pop;
        }
    }
}
